<?php
/*
 * 课程相关
 */
class CoursemainController extends BasicController {
    
    private function init(){
        Yaf_Registry::get('tokenPlugin')->checkAuth();
    }

    /*
    * 默认控制器 
    */
    public function indexAction(){
        echo '你好：'.G_USER_ID;
    }
    
    
    /*
     *	取出人机英语课程列表信息
     */
    public function listAction() {   
    	//获取所有post传递过来的参数
    	$params = $this->getPost();
    	$page = max(1, intval($params['page']));
    	$perpage = $limit = isset($params['limit']) && !empty($params['limit']) ? $params['limit'] : '10';
    	$start=(($page-1) * $perpage);
    	$where_data = [];
    	$where_data['isdelete']=0;   	
    	//年级
    	$grade_id= isset($params['grade_id']) && !empty($params['grade_id']) ? $params['grade_id'] : '';
    	if($grade_id){
    		$where_data['cm_grade_id']=$grade_id;   		
    	}
    	//学科
    	$subject_id= isset($params['subject_id']) && !empty($params['subject_id']) ? $params['subject_id'] : '';
    	if($subject_id){
    		$where_data['cm_subject_id']=$cm_subject_id;   		
    	}
    	//分类
    	$cate_id= isset($params['cate_id']) && !empty($params['cate_id']) ? $params['cate_id'] : '';		
    	if($cate_id){
    		$where_data['cm_cate_id']=$cm_cate_id;   		
    	}
    	//课程名称
    	$cm_name= isset($params['cm_name']) && !empty($params['cm_name']) ? $params['cm_name'] : '';
    	if($cm_name){
    		$where_data['cm_grade_id']=$grade_id;
    		$wheresql .= " AND cm_name LIKE '%".$cm_name."%'";
    	}
    	//是否组合课程
    	$is_group= isset($params['is_group']) && !empty($params['is_group']) ? $params['is_group'] : '0';
    	$where_data['is_group']=$is_group;
    	    
    	//付款状态
    	$cm_flag= isset($params['cm_flag'])? $params['cm_flag'] : '-1';
    	if($cm_flag!=-1){
    		$where_data['cm_flag']=$cm_flag;  		
    	}
    	$so_id='-1';
    	
    	$where_data['so_id']=$so_id; 
    
    	$cm_recommend = isset($params['cm_recommend']) ? $params['cm_recommend'] : '';
    	if ($cm_recommend) {
    		$where_data['cm_recommend']=$cm_recommend;   		
    	}
    
    	$cm_price = isset($params['cm_price']) ? $params['cm_price'] : '';
    	if ($cm_price == 1) {
    		$where_data['cm_price']='0';
    		$wheresql .= " AND cm_price = '0' ";
    	}
    	if ($cm_price == 2) {   		
    		$wheresql .= " AND cm_price > '0' ";
    	}
    
    	$k = isset($params['k']) ? $params['k'] : '';
    	$l = isset($params['l']) ? $params['l'] : '1';
    	if ($k && $l) {
    		$where_data['cm_cc_id_1_'.$l] = $k;
    	}
    
    	$cm_sort_name = isset($params['cm_sort_name']) ? $params['cm_sort_name'] : '';
    	$cm_sort_type = isset($params['cm_sort_type']) ? $params['cm_sort_type'] : '';
    	$cm_sort_name_array = array("cm_id", "cm_price", "cm_count", "good_review", "cm_price");
    	$cm_sort_type_array = array('DESC', 'ASC');
    	if ($cm_sort_name && $cm_sort_type && in_array($cm_sort_type, $cm_sort_type_array) && in_array($cm_sort_name, $cm_sort_name_array)) {
    		$orderby[$cm_sort_name] = $cm_sort_type;
    	} else {
    		$orderby['cm_id'] = "DESC";
    	}
    
    	//DEBUG 初始化请求请求获取一页列表数据的参数
   
    	
    	$Course_mainModel = Helper::load('course_main');  
    	
    	if($cm_name&&$cm_price == 2){
    		$info = $Course_mainModel->Where($where_data)->Where('cm_name','like',$cm_name)->Where(cm_price,'>','0')->Order($orderby)->Select();
    	}else if($cm_name){
    		$info = $Course_mainModel->Where($where_data)->Where('cm_name','like',$cm_name)->Order($orderby)->Select();
    	}else if ($cm_price == 2) {
    		$info = $Course_mainModel->Where($where_data)->Where(cm_price,'>','0')->Order($orderby)->Select();
    	}else{
    		$info = $Course_mainModel->Where($where_data)->Order($orderby)->Select();
    	}
    	
    	//DEBUG 调试输出
    	if($info){
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $info; 		
    		echo json_encode($return);
    		return false;
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = []; 		
    		echo json_encode($return);
    	}
    
    }
    
    /*
     *	取出一条信息
     */
    public function detailAction() {
    	$params = $this->getPost();
    	$cm_id = isset($params['cm_id']) ? $params['cm_id'] : '67824';
    	if(!$cm_id){
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	$Course_mainModel = Helper::load('course_main');
    	$detail= $Course_mainModel->Where(["cm_id" => $cm_id])->SelectOne();
    	    	    
    	//DEBUG 调试输出
    	if($detail['is_group'] ==1){    		
    		$Course_mainModel = Helper::load('course_group_lesson');
    		$detail['child_course']= $Course_mainModel->Where(["cm_id" => $cm_id,'so_id'=>'-1'])->Select(); print_r($detail['child_course']) ;  		  		    
    	}else{
    		$detail['child_course']=array();
    	}
    	if($detail){
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $detail;
    		echo json_encode($return);
    		return false;  		
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;  		
    	}
    }
    
    /*
     *  第三方添加课程
     */
    public function addAction(){
    	//获取所有post传递过来的参数
    	$params = $this->getPost();
    	
    	$course_main = isset($params['course_main']) && !empty($params['course_main'])?$params['course_main']:'';
    	if(empty($course_main)){//无效的提交参数
    		$return['code'] = 2;
    		$return['msg'] = '参数错误'; 	
    		echo json_encode($return);
    		return false;
    	}
    	//人机英语默认为-1
    	$course_main['so_id'] = -1;
    	//TODO 后端字段校验 根据具体业务逻辑添加
    	//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
    	foreach($course_main AS $key => $value){
    		if(is_array($value) && !empty($value)){
    			$course_main[$key] = implode(",", $value);
    		}
    	}
    	$course_main['create_dateline'] = CUR_TIMESTAMP;
    	$course_main['modify_dateline'] = CUR_TIMESTAMP;
    	$course_main['user_id'] = -1;    
    	$Course_mainModel = Helper::load('course_main');   	
    	$r = $Course_mainModel->Insert($course_main);    	
    	if($r){   		
    		$course= $Course_mainModel->Where(["cm_id" => $r])->SelectOne();
    		$return['code'] = 0;
    		$return['msg'] = '操作成功';
    		$return['data'] = $course; 		
    		echo json_encode($return);
    		return false;
    	}else{
    		$return['code'] = 1;
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    	    
    }
    
    /*
     *  编辑人机英语课程信息
     */
    public function editAction() {  
    	//获取所有post传递过来的参数
    	$params = $this->getPost();
    	$course_main = isset($params['course_main']) && !empty($params['course_main']) ? $params['course_main']:'';
    	$cm_id = isset($params['cm_id']) ? $params['cm_id'] : '';
    	if($cm_id){
    		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
    		foreach($course_main AS $key => $value){
    			if(is_array($value) && !empty($value)){
    				$course_main[$key] = implode(",", $value);
    			}
    		}
    		$course_main['modify_dateline'] = CUR_TIMESTAMP;
    		$Course_mainModel = Helper::load('course_main');
    		$effect_row = $Course_mainModel->Where(['cm_id' => $cm_id,'so_id'=>'-1'])->UpdateOne($course_main);
    		   	
    		if($effect_row){
    			$course= $Course_mainModel->Where(["cm_id" => $cm_id])->SelectOne();
    			$return['code'] = 0;
    			$return['msg'] = '操作成功';
    			$return['data'] = $course;
    			echo json_encode($return);
    			return false;
    			
    		}else{
    			$return['code'] = 1;
	    		$return['msg'] = '操作失败';
	    		$return['data'] = [];
	    		echo json_encode($return);
	    		return false;
    		}
    	}else{
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    }
    
    /*
     *  删除课程信息
     */
    public function deleteAction() {
    	//获取所有post传递过来的参数
    	$params = $this->getPost();
    	$cm_id = isset($params['cm_id']) ? $params['cm_id'] : '';   	
    	if($cm_id){   
    		//是否被购买
    		$Trade_courseModel = Helper::load('trade_course');
    		$is_buy = $Trade_courseModel->Where(["cm_id" => $cm_id,'tc_flag' => '2'])->SelectOne();
    		if($is_buy){
    			$return['code'] = '4';
    			$return['msg'] = '无法删除课程已被购买';
    			$return['data'] = [];
    			echo json_encode($return);
    			return false;
    		}
    		$Course_mainModel = Helper::load('course_main');
    		$uData['isdelete'] = 1;
    		$uData['modify_dateline'] = CUR_TIMESTAMP;   	
    		$effect_row = $Course_mainModel->Where(['cm_id' => $cm_id,'so_id'=>'-1'])->UpdateOne($uData);   		
    		if($effect_row){
    			$return['code'] = '0';
    			$return['msg'] = '操作成功';
    			$return['data'] = [];
    			echo json_encode($return);
    			return false;
    		}else{
    			$return['code'] = '1';
    			$return['msg'] = '操作失败';
    			$return['data'] = [];
    			echo json_encode($return);
    			return false;
    		}
    	}else{
    		$return['code'] = '3';
    		$return['msg'] = '非法参数';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    }
    
    /*
     *  添加组合课程的子课程
     */
    public function childAction(){   	
    	$params = $this->getPost();    	
    	$cg_id=isset($params['cg_id']) ? $params['cg_id']:'';
    	$cm_id = isset($params['cm_id']) && !empty($params['cm_id']) ? $params['cm_id']:[];
    	
    	if(!$cg_id){//无效的提交参数
    		$return['code'] = '2';
    		$return['msg'] = '参数错误';
    		echo json_encode($return);
    		return false;
    	}
    	//判断是否人机英语的课程
    	$Course_mainModel = Helper::load('course_main');
    	$detail= $Course_mainModel->Where(["cm_id" => $cg_id])->SelectOne();
        if($detail['so_id']!='-1'){
    		$return['code'] = '3';
    		$return['msg'] = '没有权限';
    		echo json_encode($return);
    		return false;
    	}
    	
    	$Course_group_lessonModel = Helper::load('course_group_lesson');
    	//先删除原有记录，防止重复添加
    	$r=$Course_group_lessonModel->Where(["cg_id" => $cg_id])->Delete();
    	$is_insert=[];
    	if(!empty($cm_id)){
    		foreach ($cm_id as $key => $val){
    			if($val){
    				$insert_data=array(
    						'cg_id'=>$cg_id,
    						'cm_id'=>$val
    				);
    				$is_insert[] = $Course_group_lessonModel->Insert($insert_data);    				
    			}
    		}  		
    	}
    	
        if($r||!empty($is_insert)){   		   		
    		$return['code'] = '0';
    		$return['msg'] = '操作成功';
    		$return['data'] = []; 		
    		echo json_encode($return);
    		return false;
    	}else{
    		$return['code'] = '1';
    		$return['msg'] = '操作失败';
    		$return['data'] = [];
    		echo json_encode($return);
    		return false;
    	}
    
    }
    
    
}